//
//  UserViewController.m
//  Four_Fit
//
//  Created by xalo on 16/7/12.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "UserViewController.h"
#import "EditViewController.h"
#import "SettingsTableViewController.h"


@interface UserViewController ()<UINavigationControllerDelegate ,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;        //头像
@property (weak, nonatomic) IBOutlet UIButton *editBtn;             //编辑按钮
@property (weak, nonatomic) IBOutlet UILabel *IDLable;              //账号
@property (weak, nonatomic) IBOutlet UILabel *genderLable;          //性别
@property (weak, nonatomic) IBOutlet UILabel *ageLable;             //年龄
@property (weak, nonatomic) IBOutlet UILabel *weightLable;          //体重
@property (weak, nonatomic) IBOutlet UILabel *PurposeLable;         //宣言
@property (nonatomic, strong) UIImagePickerController *imagePick;   //添加相机

@end

@implementation UserViewController

- (UIImagePickerController *)imagePick {
    
    if (!_imagePick) {
        
        _imagePick = [[UIImagePickerController alloc]init];
        _imagePick.delegate = self;
        _imagePick.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    return _imagePick;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    tap.numberOfTapsRequired = 1;
    [self.iconImage addGestureRecognizer:tap];
    self.iconImage.userInteractionEnabled = YES;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightAction:)];
}

- (void)tapAction:(UITapGestureRecognizer *)sender {
    
    //底部提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"选择图片来源" preferredStyle:UIAlertControllerStyleActionSheet];
    //添加相机按钮
    UIAlertAction *takePhoto = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            [self presentViewController:self.imagePick animated:YES completion:nil];
        } else {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是" message:@"否" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:^{
                [self performSelector:@selector(removealertView:) withObject:alert afterDelay:0.8];
            }];
        }
    }];
    [alert addAction:takePhoto];
    
    //添加相册按钮
    UIAlertAction *pickPhoto = [UIAlertAction actionWithTitle:@"从本地提取相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            self.imagePick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:self.imagePick animated:YES completion:nil];
        }
    }];
    [alert addAction:pickPhoto];
    
    //添加取消
    UIAlertAction *actioncancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:actioncancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)removealertView:(UIAlertController *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)rightAction:(UIBarButtonItem *)sender {
    
    SettingsTableViewController *settingsVC = [[SettingsTableViewController alloc]init];
    UINavigationController *settings = [[UINavigationController alloc]initWithRootViewController:settingsVC];
    [self.navigationController presentViewController:settings animated:YES completion:nil];
}
//编辑按钮点击事件
- (IBAction)editBtnAction:(id)sender {
    
    EditViewController *editVC = [[EditViewController alloc]init];
    UINavigationController *edit = [[UINavigationController alloc]initWithRootViewController:editVC];
    [self.navigationController presentViewController:edit animated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
